//禁用手机默认的触屏滚动行为
export const touchFalse = function () {
	document.addEventListener('touchmove', touchDefault, {
		passive: false
	});
}
function touchDefault(event) {
	event.preventDefault();
}
//恢复手机默认的触屏滚动行为
export const touchTrue =function() {
	document.removeEventListener('touchmove', touchDefault, {
		passive: false
	});
}

// 获取地址栏

export function getParames(name) {
  // console.log(location)
  var after = window.location.hash.split("?")[1];
  if (after) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = after.match(reg);
    if (r != null) return decodeURIComponent(r[2]);
    return null;
  }
}
//判断设备
export function iosSystem() {
  let iosSys = false;
  let uA = navigator.userAgent;
  let isAndroid = uA.indexOf('Android') > -1 || uA.indexOf('Adr') > -1; //android终端
  let isiOS = !!uA.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
  if (!isAndroid) { //ios终端
    iosSys = true;
  }
  return iosSys;
}


export function touchDefault(event) {

	event.preventDefault();
}

// 兼容ios时间计算
export const timeFamter = (date = new Date()) => {
	var newDate=new Date(date).getTime()
	if(iosSystem() && !newDate){
		newDate=new Date(date.replace(/-/g, "/")).getTime();
	}
	return newDate
}


// 防抖动
export const debounce = (fn,delay = 500) => {
	let timer = null;
	return function(){        
		var args = arguments;
		clearTimeout(timer);
		timer = setTimeout(() => {
			fn.apply(this,args);
		},delay)
	}
}


// 预加载序列图片
export const loadImages = (sources,callback) =>{
	var loadedImages = 0;
	var numImages = 0;
	var images=[];
	numImages=sources.length;
	for (var i=0,len=sources.length;i<len;i++) {
		images[i] = new Image();
		//当一张图片加载完成时执行
		images[i].onload = function(){
			//当所有图片加载完成时，执行回调函数callback
			if (++loadedImages >= numImages) {
				callback(images);
			}
		};
	//     //把sources中的图片信息导入images数组
		images[i].src = sources[i]
	}
}
// 播放图片动画
export const playImages = (images,canData,callback) => {
	const {cxt, width, height} = canData
	var imageNum=images.length;
	var imageNow=0;
	setInterval(function(){
		cxt.clearRect(0,0, width, height);
		cxt.fillRect(0, 0, width, height);
		cxt.drawImage(images[imageNow], 0, 0, width, height);
		imageNow++;
		if(imageNow>=imageNum){
			imageNow=imageNum-1;
			callback()
		}
	},100)
}
export const playAnimeImages = (images,canData,callback) => {
	const {cxt, width, height, stepAct, step} = canData
	var imageNow = 0
	var rafId = null
	var imageNum=images.length;
	//当前执行时间
	var nowTime = 0;
	//记录每次动画执行结束的时间
	var lastTime = Date.now();
	//定义的动画时间差值
	var diffTime = 50;
	var animloop = function (){
		nowTime = Date.now()
		cxt.clearRect(0,0, width, height);
		cxt.fillRect(0, 0, width, height);
		cxt.drawImage(images[imageNow], 0, 0, width, height);

		if(stepAct){
			if((!step||(step&&imageNow != step.val))&&nowTime-lastTime > diffTime){
				lastTime = nowTime
				if(imageNow < imageNum-1){
					imageNow++;
				}else{
					callback()
					cancelAnimationFrame(rafId)
				}
			}
			if(step && imageNow == step.val && (nowTime-lastTime) > step.time){
				imageNow++;
			}
		}else{
			cancelAnimationFrame(rafId)
		}
		rafId = requestAnimationFrame(animloop);
	}
	
	animloop()
	
}
